<template>
  <el-container style="height: 100vh;">
    <!-- 侧边栏 -->
    <el-aside
  :width="isCollapse ? '64px' : '200px'"
  @mouseenter="handleMouseEnter"
  @mouseleave="handleMouseLeave"
>
<div class="menu-title" :class="{ collapsed: isCollapse }">
  <template v-if="!isCollapse">管理系统</template>
  <template v-else>管</template> <!-- 缩略名或 logo -->
</div>



      <el-menu
  :default-active="activeMenu"
  class="el-menu-vertical"
  background-color="#304156"
  text-color="#fff"
  active-text-color="#409EFF"
  :collapse="isCollapse"
  @select="handleMenuSelect"
>
  <el-menu-item index="AdminIndex">
    <el-icon><House /></el-icon>
    <span v-if="!isCollapse" class="menu-title-text">首页</span>
  </el-menu-item>

  <el-sub-menu index="1">


    
    <template #title>
      <el-icon><Message /></el-icon>
      <span v-if="!isCollapse" class="menu-title-text">反馈管理</span>
    </template>
    <el-menu-item index="admin-feedback-experience">用户体验反馈</el-menu-item>
    <el-menu-item index="admin-feedback-question">答题疑惑反馈</el-menu-item>
    <el-menu-item index="admin-feedback-responses">我的反馈回复</el-menu-item>
  </el-sub-menu>

  <el-menu-item index="admin-questions">
    <el-icon><Edit /></el-icon>
    <span v-if="!isCollapse" class="menu-title-text">题目管理</span>
  </el-menu-item>

  <el-sub-menu index="4">
    <template #title>
      <el-icon><ChatDotRound /></el-icon>
      <span v-if="!isCollapse" class="menu-title-text">社区管理</span>
    </template>
    <el-menu-item index="admin-discussion-topics">讨论主题管理</el-menu-item>
  </el-sub-menu>
  <el-menu-item index="admin-user-info">
  <el-icon><User /></el-icon>
  <span v-if="!isCollapse" class="menu-title-text">用户管理</span>
</el-menu-item>
  <el-menu-item index="admin-logs">
    <el-icon><Document /></el-icon>
    <span v-if="!isCollapse" class="menu-title-text">操作日志</span>
  </el-menu-item>

  <el-menu-item index="admin-info">
    <el-icon><UserFilled /></el-icon>
    <span v-if="!isCollapse" class="menu-title-text">管理员信息</span>
  </el-menu-item>
    <el-menu-item index="syslogs">
    <el-icon><Edit /></el-icon>
    <span v-if="!isCollapse" class="menu-title-text">登录日志管理</span>
  </el-menu-item>
</el-menu>

    </el-aside>

    <el-container>
      <el-header style="height: 100px;">
        <div style="display: flex; justify-content: space-between; align-items: center; height: 100%;">
          <h2 style="margin: 0;">管理员后台</h2>
          <div class="user-info">
            <span class="welcome-text">{{ adminName }}</span>
            <el-button type="primary" @click="handleLogout" class="logout-btn">退出登录</el-button>
          </div>
        </div>
      </el-header>
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { ElMessage } from 'element-plus'
import { useUserStore } from '../../store/UserStore'
import {
  Message,
  Edit,
  User,
  Document,
  UserFilled,
  ArrowDown,
  House,
  ChatDotRound
} from '@element-plus/icons-vue'

export default {
  name: 'AdminHome',
  components: {
    Message,
    Edit,
    User,
    Document,
    UserFilled,
    ArrowDown,
    House,
    ChatDotRound
  },
  data() {
    return {
      activeMenu: this.$route.name || 'admin-dashboard',
      isCollapse: true,
      collapseTimeout: null,
      adminName: '管理员'
    }
  },
  methods: {
    handleMouseEnter() {
      if (this.collapseTimeout) {
        clearTimeout(this.collapseTimeout)
      }
      this.isCollapse = false
    },
    handleMouseLeave() {
      this.collapseTimeout = setTimeout(() => {
        this.isCollapse = true
      }, 500)
    },
    handleMenuSelect(key) {
      console.log('菜单项被点击，key:', key)
      
      // 根据菜单项的路由名称进行跳转
      const routeMap = {
        'admin-user-info': '/admin/users',
        'admin-info': '/admin/profile',
        'syslogs': '/admin/system-logs',
        'admin-feedback-experience': '/admin/feedback/experience',
        'admin-feedback-question': '/admin/feedback/question',
        'admin-feedback-responses': '/admin/feedback/responses',
        'admin-questions': '/admin/questions',
        'admin-discussion-topics': '/admin/discussion-topics',
        'admin-logs': '/admin/logs',
        'AdminIndex': '/admin'
      }

      console.log('当前路由映射:', routeMap)
      console.log('目标路由:', routeMap[key])

      if (routeMap[key]) {
        console.log('准备跳转到:', routeMap[key])
        try {
          this.$router.push(routeMap[key])
          console.log('路由跳转成功')
        } catch (error) {
          console.error('路由跳转失败:', error)
        }
      } else {
        console.warn('未找到对应的路由映射:', key)
      }
    },
    handleLogout() {
      const userStore = useUserStore()
      userStore.logout()
      this.$router.push('/login')
      ElMessage.success('已退出登录')
    },
    async fetchAdminInfo() {
      try {
        const userStore = useUserStore()
        this.adminName = userStore.userInfo?.username || '管理员'
      } catch (error) {
        console.error('获取管理员信息失败:', error)
        ElMessage.error('获取管理员信息失败')
      }
    }
  },
  mounted() {
    this.fetchAdminInfo()
  }
}
</script>

<style scoped>
.menu-title {
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  color: #fff;
  border-bottom: 1px solid #1f2d3d;
}
.el-aside {
  background-color: #304156 !important;
  transition: width 0.3s ease;
  overflow: hidden;
  border-right: none;
}

.el-header {
  background-color: #fff;
  color: #333;
  line-height: 100px;
  border-bottom: 1px solid #e6e6e6;
  padding: 0 20px;
}
.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
  display: flex;
  align-items: center;
  font-size: 14px;
}
.el-dropdown-link:hover {
  color: #66b1ff;
}
.el-icon--right {
  margin-left: 4px;
}
.menu-title {
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  color: #fff;
  border-bottom: 1px solid #1f2d3d;
  transition: all 0.3s ease;
  overflow: hidden;
  white-space: nowrap;
}

.menu-title.collapsed {
  font-size: 0;
  opacity: 0;
  height: 0;
  padding: 0;
  border: none;
}

.menu-title {
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  color: #fff;
  border-bottom: 1px solid #1f2d3d;
  transition: all 0.2s ease;
  overflow: hidden;
  white-space: nowrap;
}

.menu-title.collapsed {
  font-size: 16px;
}

.el-menu-vertical {
  background-color: #304156 !important;
  border-right: none;
}

.el-menu {
  box-sizing: border-box;
  transition: width 0.3s ease;
  border-right: none !important;
}

.menu-title-text {
  display: inline-block;
  white-space: nowrap;
  overflow: hidden;
  transition: opacity 0.3s ease, max-width 0.3s ease, margin-left 0.3s ease;
  max-width: 150px;
  margin-left: 10px;
  opacity: 1;
}

/* 折叠状态下隐藏文字 */
.el-menu--collapse .menu-title-text {
  max-width: 0;
  margin-left: 0;
  opacity: 0;
}

.admin-home {
  height: 100vh;
  background-color: #f5f7fa;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-img {
  height: 40px;
  width: auto;
}

.system-name {
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.welcome-text {
  font-size: 14px;
  color: #666;
}

.logout-btn {
  padding: 8px 20px;
  font-size: 14px;
}
</style>
